﻿
var queryRuleUrl = ArrangeUrl + "/DoctorScheduling/getDoctorScheduling";
var exportPbRuleUrl = ArrangeUrl + "/DoctorScheduling/outExcel";


$(function () {
    loadArrangeRuleTableData();
})


function exportArrangeRule() {
    window.open(exportPbRuleUrl);
}

//初始化排班规则
function loadArrangeRuleTableData() {
    //异步加载数据
    //传页码根据条件查询
    var _index = null;
    $("#table_arrange_rule").empty();
    $.ajax({
        url: queryRuleUrl,
        data: {
            deptId: $("#dept_infolist").val(),//"b921507f74a04d50a83365fc40c26fea"//_deptId $("#dept_infolist").val()
        },
        type: 'get',
        beforeSend: function () {
            _index = layer.msg("正在加载数据", { icon: 16 });
        },
        success: function (d) {
            if (JSON.parse(d).status == '0') {
                $("#table_arrange_rule").html("");
                var _rulejson = JSON.parse(d);
                if (_rulejson.data == null)
                    return;

                /**
                 * 获取排版字典
                 */
                schedulingDictionary();

                //不传页码差全部
                var trHtml = '';//tr拼接
                for (var i = 0; i < _rulejson.data.length; i++) {
                    var _jsonObject = _rulejson.data[i];
                    var _doctorId = _jsonObject.doctorId;
                    var _doctorKey = _jsonObject.doctorKey;
                    var _doctorName = _jsonObject.doctorName;
                    var _doctorTitle = _jsonObject.doctorTitle;
                    var _schedulingKey = _jsonObject.schedulingKey == null ? "0000000" : _jsonObject.schedulingKey;
                    // var _doctorTitleDesc = _doctorTitle == "1" ? "住院医师" : _doctorTitle == "2" ? "主治医师"
                    //     : _doctorTitle == "3" ? "副主任医师" : _doctorTitle == "4" ? "主任医师" : "未设置";

                    var _doctorTitleDesc = _jsonObject.doctorTitleName || ''
                    trHtml += "<tr id='"+ JSON.stringify(_jsonObject) +"'>" +
                        '<td><span>' + _doctorKey + '</span></td>' + //工号
                        '<td><span>' + _doctorName + '</span></td>' + //医生
                        '<td><span>' + _doctorTitleDesc + '</span></td>' + //职称
                        getSchedulingKeyHtml(JSON.parse(_schedulingKey)) +  //周一~周日午别选择
                        '</tr>';
                }
                $("#table_arrange_rule").html(trHtml);
                $('.multipleSelected').multiselect({
                    nonSelectedText: '休息',
                    buttonWidth: 107,
                    numberDisplayed:'100',//显示个数设置为最大
                    allSelectedText:'' //全选text设置为空，就可以超过四个也显示了
                });
                $('.multipleSelected').multiselect('refresh');
            } else {
                $("#table_arrange_rule").html("");
            }
        }
    });
}

/**
 *  拼接共用select下拉框
 *  @param scheduArr Array
 *  @return  {string}
 *  新增0580海原县排版字典
 */
function getSchedulingKeyHtml(scheduArr) {
    scheduArr = scheduArr ||[];
    var scheduHtml = '';
    for(var g = 0; g < scheduArr.length; g++){
        scheduHtml += '<td>';
        scheduHtml += '<select id="multiple-selected_'+ g +'" multiple="multiple" class="multipleSelected" size="2">';
        scheduHtml += (function (index) {
            return initSelectOption(schedulingdicList,scheduArr[index]);
        })(g);
        scheduHtml += '</select>';
        scheduHtml += '</td>';
    }
    return scheduHtml;
}

/***
 *  根据返回值比较勾选与否
 *  新增0580海原县排版字典
 * */
function initSelectOption(dataList,scheduArr) {
    scheduArr = scheduArr || [];
    var _schedulingKey,_schedulingName,frag='';
    for(var i = 0; i < dataList.length; i++){
        _schedulingKey = dataList[i].dicKey;
        _schedulingName = dataList[i].dicName;
        if(isHas(scheduArr,_schedulingKey)){
            frag += "<option selected value='"+ _schedulingKey +"'>"+ _schedulingName +"</option>";
        }else {
            frag += "<option value='"+ _schedulingKey +"'>"+ _schedulingName +"</option>";
        }
    }
    return frag;
}

/**
 * scheduArr数组中的值和dicKey比较，==及选中勾选
 * @param scheduArr：通过排班字典data数据获取
 * @param key：通过排班规则列表接口获取schedulingKey
 * @returns {boolean}
 * 新增0580海原县排版字典
 */
function isHas(scheduArr,key) {
    var bool = false;
    for(var i = 0; i < scheduArr.length; i++) {
        if (scheduArr[i] == key) {
            bool = true;
            break;
        }
    }
    return bool;
}


function InitSelectColor(_this){
    var i=0;
    $(_this).children().each(function () {
        i++;
        if(i>3){
            $(this).find('button').css({"background-color":$(this).find("select").css("background-color")});
        }
    });
}

//编辑
function editTemplate(d) {
    console.log(d);
    window.location.href = '../information_publish/new_instance.html?instanceId=' + d;
}

//删除
function deleteTemplate(d) {
    layer.confirm('确定要删除？', {
        btn: ['取消', '确定'],
        icon: 3,
    }, function () {
        layer.closeAll();
        //刷新表
        loadArrangeRuleTableData();

    }, function () {

        layer.open({
            type: 1,
            skin: 'layui-layer-demo', //样式类名
            closeBtn: 0, //不显示关闭按钮
            anim: 2,
            shadeClose: true, //开启遮罩关闭
            content: '内容'
        });
    });
}

//初始化分页条
function loadTemplateTablePagin(jData) {
    //异步加载数据
    var _startNumPage = jData.pageNum;
    var _sumNumPage = jData.pages;

    Page({
        num: _sumNumPage,					        //总页码数
        startnum: _startNumPage,				    //指定页码
        elem: $('#template_pagebar'),	            //指定的元素
        callback: function (n) {	                //回调函数
            console.log(n);
            loadArrangeRuleTableData(n);
        }
    });
}

//新建节目
function newupload() {
    window.location.href = '../information_publish/new_instance.html';
    //layer.open({
    //    type: 2,//加载外部页面类型
    //    closeBtn: 1,
    //    title: "上传模板",//不显示标题栏
    //    // shadeClose: true,//点其他地方自动关闭
    //    area: ['500px', '300px'],
    //    content: '../information_publish/newupload_template.html',
    //});
}
